package com.panshi.controller;


import com.panshi.IssApi;
import com.panshi.api.UserPoth;
import com.panshi.api.api.ArticleNoPort;
import com.panshi.api.api.ArticlePort;
import com.panshi.api.api.ReviewsPort;
import com.panshi.api.api.TypePort;
import com.panshi.api.bean.*;
import com.panshi.api.util.PageSizeVO;
import com.panshi.bean.UserVO;
import com.panshi.util.CommonUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@Slf4j
public class ConsultController {
    @Autowired
    private CommonUtil commonUtil;
    @Autowired
    private ArticlePort articlePort;

    @Autowired
    private TypePort typePort;

    @Autowired
    private ReviewsPort reviewsPort;

    @Autowired
    private ArticleNoPort articleNoPort;

    @Autowired
    private IssApi issApi;

    @Autowired
    private UserPoth userPoth;

    @GetMapping("/consult/main")
    public String consultMain(Model model, HttpServletRequest request,Integer indexPage,String list){
        model.addAttribute("zx","zx");
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        model.addAttribute("auto",auto_login);
        System.out.println("===========================================");
        System.out.println(auto_login);
        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));//查询用户
            model.addAttribute("user",userQuery);
        }
        PageSizeVO pageSizeBean = new PageSizeVO();
        pageSizeBean.setStartIndex(1);
        pageSizeBean.setPageSize(10);

        List<ArticleBean> yb_article_vos=null;
        if(indexPage==null && list==null){//推荐
           yb_article_vos = articlePort.queryArticleInfo(pageSizeBean);
        }else if("10".equals(list)){//热门
            yb_article_vos=articlePort.queryHotArticleInfo(pageSizeBean);
        }else if("11".equals(list)){//最新
            yb_article_vos=articlePort.queryNewArticleInfo(pageSizeBean);
        }


        Collections.shuffle(yb_article_vos);//打乱集合
        System.out.println(yb_article_vos.size()+"-----首页");

        model.addAttribute("djList",articlePort.randomQueryArticle(pageSizeBean));//大家都在看
        model.addAttribute("consultType",list);
        model.addAttribute("consultList",yb_article_vos);
        pageSizeBean.setPageSize(4);
        System.out.println(articlePort.randomQueryArticle(pageSizeBean).size()+"专家推荐");
        model.addAttribute("zjList",articlePort.randomQueryArticle(pageSizeBean));//专家推荐
        model.addAttribute("typeList",typePort.queryArticleInfoAll());//牙齿类型


        return "consult/consult_main";
    }

    @GetMapping("/consult/type")
    public String cosultMainType(Model model, HttpServletRequest request, int type, String list){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        model.addAttribute("auto",auto_login);
        model.addAttribute("zx","zx");
        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));//查询用户
            model.addAttribute("user",userQuery);
        }
        PageSizeVO pageSizeBean = new PageSizeVO();
        pageSizeBean.setStartIndex(1);
        pageSizeBean.setPageSize(10);
        pageSizeBean.setTid(type);


        List<ArticleBean> arr=null;

        if(list == null){//全部
            arr=articlePort.queryArticleInfo(pageSizeBean);
        }else if("10".equals(list)){//热门
            arr=articlePort.queryHotArticleInfo(pageSizeBean);
        }else if("11".equals(list)){//最新
            arr=articlePort.queryNewArticleInfo(pageSizeBean);
        }
        Collections.shuffle(arr);//打乱集合


        model.addAttribute("toothType",type);//当前牙齿类型
        model.addAttribute("consultList",list);//文章最新，最热，推荐类型
        model.addAttribute("consultArr",arr);//文章数据
        model.addAttribute("typeList",typePort.queryArticleInfoAll());//牙齿类型
        model.addAttribute("tjList",articlePort.randomQueryArticle(pageSizeBean));//相关推荐
        pageSizeBean.setPageSize(5);
        model.addAttribute("qtList",articlePort.randomQueryArticle(pageSizeBean));//其他推荐
        return "consult/consult_main_type";
    }

    @GetMapping("/consult/solo/{id}")
    public String consultSolo(@PathVariable int id,Model model, HttpServletRequest request){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        model.addAttribute("auto",auto_login);
        if(auto_login.length()>0){
            UserVO userQuery = userPoth.findUserQuery(Integer.valueOf(auto_login));//查询用户
            model.addAttribute("user",userQuery);
        }
        PageSizeVO pageSizeBean = new PageSizeVO();
        pageSizeBean.setStartIndex(1);
        pageSizeBean.setPageSize(10);



        ArticleBean articleBean = articlePort.queryAssignArticleInfo(id);
        model.addAttribute("article",articleBean);
        model.addAttribute("typeList",typePort.queryArticleInfoAll());//牙齿类型
        model.addAttribute("qtList",articlePort.randomQueryArticle(pageSizeBean));//其他看点

        pageSizeBean.setTid(articleBean.getTid());//设置类型

        List<ArticleBean> articleBeans = articlePort.queryArticleInfo(pageSizeBean);
        Collections.shuffle(articleBeans);//打乱集合
        model.addAttribute("xgList",articleBeans);//相关推荐

        pageSizeBean.setAid(id);
        pageSizeBean.setTid(0);

        return "consult/consult_particulars";
    }


    @PostMapping("/consult/add")
    @ResponseBody
    public String add(ArticleNoBean articleBean,HttpServletRequest request){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        articleBean.setUid(Integer.valueOf(auto_login));
        String str=articleNoPort.addArticNoInfo(articleBean);

        return str;
    }

    @PostMapping("/consult/sc")
    @ResponseBody
    public String sc(UserCollectionBean userCollectionBean,HttpServletRequest request){
        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");
        if(auto_login == "" || auto_login.length() == 0 || auto_login == null){
            return "404";
        }
        userCollectionBean.setUid(Integer.valueOf(auto_login));

        if("0".equals(userCollectionBean.getType_id())){//文章
            String var=articlePort.addCollectSum(userCollectionBean);
            return var;
        }else if("1".equals(userCollectionBean.getType_id())){//问题
            int i=issApi.initColl(Integer.valueOf(auto_login),userCollectionBean.getAid());
            return i+"";
        }
        return "404";
    }

    @PostMapping("/consult/append")
    @ResponseBody
    public Object zxAppend(String current, String type, String cateId, ModelAndView modelAndView){

        PageSizeVO pageSizeBean = new PageSizeVO();
        pageSizeBean.setStartIndex(Integer.valueOf(current));
        pageSizeBean.setPageSize(10);

        List<ArticleBean> yb_article_vos=null;

        if(type==null || type==""){//推荐
            yb_article_vos= articlePort.queryArticleInfo(pageSizeBean);
        }else if("10".equals(type)){//热门
            yb_article_vos= articlePort.queryHotArticleInfo(pageSizeBean);
        }else if("11".equals(type)){//最新
            yb_article_vos=articlePort.queryNewArticleInfo(pageSizeBean);
        }else if("105".equals(type)){//分类
            if(cateId!=null || cateId!=""){
                pageSizeBean.setTid(Integer.valueOf(cateId));
            }
            yb_article_vos= articlePort.queryArticleInfo(pageSizeBean);
        }
        else{
            yb_article_vos=articlePort.randomQueryArticle(pageSizeBean);
        }

        if(yb_article_vos==null||yb_article_vos.size()==0){
            return "";
        }

        Collections.shuffle(yb_article_vos);//打乱集合
        yb_article_vos.forEach(var-> System.out.println(var.getA_title()));

        modelAndView.addObject("zxList", yb_article_vos);	//为ModelAndView变量设置Model(Model即数据)
        modelAndView.setViewName("consult/consult_page");		//为ModelAndView变量设置View(该视图View 为/WEB-INF/show.jsp)

        return modelAndView;
    }

    @PostMapping("/consult/pl")
    @ResponseBody
    public ModelAndView pinlun(PageSizeVO pageSizeVO, ModelAndView modelAndView){

        pageSizeVO.setPageSize(5);

        List<ArticleReviewsBean> articleReviewsBeans;


        if(pageSizeVO.getAid()!=0){
            articleReviewsBeans = reviewsPort.queryAssginArticleReviewsInfo(pageSizeVO);
            modelAndView.addObject("pid",pageSizeVO.getParentid());
        }else{
            articleReviewsBeans=reviewsPort.queryAssginArticleReplyInfo(pageSizeVO);
            modelAndView.addObject("pid",1);
        }




        modelAndView.addObject("commentList",articleReviewsBeans);

        modelAndView.setViewName("common/hasbei_comment_join");
        return modelAndView;
    }




    @PostMapping("/consult/dianzan")
    @ResponseBody
    public String dianzan(PraoseLinkBean praoseLinkBean,HttpServletRequest request){

        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");

        if(auto_login=="" ||auto_login==null ||auto_login.length()==0){
            return "no";
        }
        praoseLinkBean.setUid(Integer.valueOf(auto_login));
        praoseLinkBean.setState(1);
        int s=reviewsPort.addArticlePraise(praoseLinkBean);
        return s+"";
    }

    @PostMapping("/consult/addComment")
    @ResponseBody
    public Object addComment(HttpServletRequest request, HttpServletResponse response, ArticleReviewsBean articleReviewsBean, ModelAndView modelAndView) throws IOException {

        String auto_login = commonUtil.getCookie(request, "1b12ebc9bf976801fd564c64867366c5");

        if(auto_login=="" ||auto_login==null ||auto_login.length()==0){
            //response.sendRedirect("/login");
           /* Map map=new HashMap();
            map.put("code","111");//未登录*/
            return "no";
        }

        articleReviewsBean.setUid(Integer.valueOf(auto_login));

        ArticleReviewsBean articleReviewsBean1 = reviewsPort.addReviewsInfo(articleReviewsBean);

        if(articleReviewsBean.getParentid()==0){
            modelAndView.addObject("oneReViews",articleReviewsBean1);
        }else{
            if((articleReviewsBean1.getParentid()+"").equals(articleReviewsBean.getDepth())){
                modelAndView.addObject("twoReViews",articleReviewsBean1);
            }else{
                modelAndView.addObject("threeReViews",articleReviewsBean1);
            }
        }

       modelAndView.setViewName("common/hasbei_comment_join");
        return modelAndView;
    }


    @PutMapping("/consult/update")
    @ResponseBody
    public String consultUpdate(ArticleNoBean articleNoBean,String type){

        if("yes".equals(type)){//修改通过的
            articleNoPort.addArticNoInfo(articleNoBean);
        }else{//修改正在审核的
            articleNoPort.updateAssignArticleNoInfo(articleNoBean);
        }

        return "ok";

    }

    @PostMapping("/consult/del")
    @ResponseBody
    public String consultDel(int aid ,String type){

        if("no".equals(type)){
            articleNoPort.deleteArticleNoInfo(aid);
        }else{
            articlePort.deleteArticleInfo(aid);
        }

        return "ss";
    }



}
